/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.awt.Color;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyledDocument;
import parser.SyntaxAnalyzer;

/**

 @author FiLiP
 */
public class MainFrame extends javax.swing.JFrame
{
	private StyledDocument sd;
	private SimpleAttributeSet attr = new SimpleAttributeSet();
	private String input;
	
	/**
	 Creates new form MainFrame
	 */
	public MainFrame()
	{
		initComponents();
		radioGroup.add(radioBtn1);
		radioGroup.add(radioBtn2);
		radioGroup.add(radioBtn3);
		radioGroup.add(radioBtn4);
		radioGroup.add(radioBtn5);
		radioGroup.add(radioBtn6);
		radioGroup.add(radioBtn7);
		
		sd = textInput.getStyledDocument();
		StyleConstants.setForeground(attr, Color.red);
	}

	/**
	 This method is called from within the constructor to
	 initialize the form.
	 WARNING: Do NOT modify this code. The content of this method is
	 always regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        radioGroup = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        textInput = new javax.swing.JTextPane();
        jScrollPane3 = new javax.swing.JScrollPane();
        textStackTrace = new javax.swing.JTextArea();
        btnAnalyze = new javax.swing.JButton();
        jScrollPane4 = new javax.swing.JScrollPane();
        textOutput = new javax.swing.JTextArea();
        radioBtn1 = new javax.swing.JRadioButton();
        radioBtn2 = new javax.swing.JRadioButton();
        radioBtn3 = new javax.swing.JRadioButton();
        radioBtn4 = new javax.swing.JRadioButton();
        radioBtn5 = new javax.swing.JRadioButton();
        radioBtn6 = new javax.swing.JRadioButton();
        radioBtn7 = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("LittleXML Parser");

        textInput.setFont(new java.awt.Font("Consolas", 0, 12)); // NOI18N
        jScrollPane1.setViewportView(textInput);

        textStackTrace.setColumns(20);
        textStackTrace.setFont(new java.awt.Font("Consolas", 0, 12)); // NOI18N
        textStackTrace.setRows(5);
        jScrollPane3.setViewportView(textStackTrace);

        btnAnalyze.setText("Analyze");
        btnAnalyze.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                btnAnalyzeActionPerformed(evt);
            }
        });

        textOutput.setColumns(20);
        textOutput.setFont(new java.awt.Font("Consolas", 0, 12)); // NOI18N
        textOutput.setRows(5);
        jScrollPane4.setViewportView(textOutput);

        radioBtn1.setText("Test 1");
        radioBtn1.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                radioBtn1ActionPerformed(evt);
            }
        });

        radioBtn2.setText("Test2");
        radioBtn2.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                radioBtn2ActionPerformed(evt);
            }
        });

        radioBtn3.setText("Test 3");
        radioBtn3.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                radioBtn3ActionPerformed(evt);
            }
        });

        radioBtn4.setText("Test 4");
        radioBtn4.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                radioBtn4ActionPerformed(evt);
            }
        });

        radioBtn5.setText("Test 5");
        radioBtn5.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                radioBtn5ActionPerformed(evt);
            }
        });

        radioBtn6.setText("Test 6");
        radioBtn6.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                radioBtn6ActionPerformed(evt);
            }
        });

        radioBtn7.setText("Test 7");
        radioBtn7.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                radioBtn7ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane4)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(radioBtn1)
                        .addGap(18, 18, 18)
                        .addComponent(radioBtn2)
                        .addGap(18, 18, 18)
                        .addComponent(radioBtn3)
                        .addGap(18, 18, 18)
                        .addComponent(radioBtn4)
                        .addGap(18, 18, 18)
                        .addComponent(radioBtn5)
                        .addGap(18, 18, 18)
                        .addComponent(radioBtn6)
                        .addGap(18, 18, 18)
                        .addComponent(radioBtn7)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnAnalyze))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 437, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 410, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 368, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane4, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAnalyze)
                    .addComponent(radioBtn1)
                    .addComponent(radioBtn2)
                    .addComponent(radioBtn3)
                    .addComponent(radioBtn4)
                    .addComponent(radioBtn5)
                    .addComponent(radioBtn6)
                    .addComponent(radioBtn7))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnAnalyzeActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnAnalyzeActionPerformed
    {//GEN-HEADEREND:event_btnAnalyzeActionPerformed
//		sd.setCharacterAttributes(0, 5, attr, false);
		textStackTrace.setText("");
		textOutput.setText("");
		
		input = textInput.getText().replace("\n", "").replace("\r", "").replace("\t", "");
		SyntaxAnalyzer parser = new SyntaxAnalyzer(input, this);
		if (parser.analyze() == false)
		{
			addTextToOutput("Error occurred.");
		}
		else
		{
			addTextToOutput("Parsing successful.");
		}
    }//GEN-LAST:event_btnAnalyzeActionPerformed

    private void radioBtn1ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_radioBtn1ActionPerformed
    {//GEN-HEADEREND:event_radioBtn1ActionPerformed
        textInput.setText("<empty/>");
    }//GEN-LAST:event_radioBtn1ActionPerformed

    private void radioBtn2ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_radioBtn2ActionPerformed
    {//GEN-HEADEREND:event_radioBtn2ActionPerformed
        textInput.setText("<?xml version=12.34?>\n<empty/>");
    }//GEN-LAST:event_radioBtn2ActionPerformed

    private void radioBtn3ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_radioBtn3ActionPerformed
    {//GEN-HEADEREND:event_radioBtn3ActionPerformed
        textInput.setText("<?xml version=0.1?>\n<full>lorem ipsum dolor </full>");
    }//GEN-LAST:event_radioBtn3ActionPerformed

    private void radioBtn4ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_radioBtn4ActionPerformed
    {//GEN-HEADEREND:event_radioBtn4ActionPerformed
        textInput.setText("<full>\n\t<level1>\n\t\t<level2/>\n\t</level1>\n</full>");
    }//GEN-LAST:event_radioBtn4ActionPerformed

    private void radioBtn5ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_radioBtn5ActionPerformed
    {//GEN-HEADEREND:event_radioBtn5ActionPerformed
        textInput.setText("<?xml version=0.1?>\n<full>\n\t<level1>lorem ipsum dolor </level1>\n</full>");
    }//GEN-LAST:event_radioBtn5ActionPerformed

    private void radioBtn6ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_radioBtn6ActionPerformed
    {//GEN-HEADEREND:event_radioBtn6ActionPerformed
        textInput.setText("<?xml version=0.1?>\n<_underScored/>");
    }//GEN-LAST:event_radioBtn6ActionPerformed

    private void radioBtn7ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_radioBtn7ActionPerformed
    {//GEN-HEADEREND:event_radioBtn7ActionPerformed
        textInput.setText("<:colonElement/>");
    }//GEN-LAST:event_radioBtn7ActionPerformed

	/**
	 @param args the command line arguments
	 */
	public static void main(String args[])
	{
		/*
		 Set the Nimbus look and feel
		 */
		//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
		 If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
		 For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
		 */
		try
		{
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels())
			{
				if ("Nimbus".equals(info.getName()))
				{
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		}
		catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex)
		{
			java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
		}
		//</editor-fold>

		/*
		 Create and display the form
		 */
		java.awt.EventQueue.invokeLater(new Runnable()
		{
			public void run()
			{
				new MainFrame().setVisible(true);
			}
		});
	}
	
	public void addTextToOutput(String text)
	{
		SimpleDateFormat df = new SimpleDateFormat("dd.MM.YYYY HH:mm:ss ");
		textOutput.append(df.format(new Date()) + text + "\n");
	}
	
	public void addTextToStackTrace(String text)
	{
		textStackTrace.append(text + "\n");
	}
	
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAnalyze;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JRadioButton radioBtn1;
    private javax.swing.JRadioButton radioBtn2;
    private javax.swing.JRadioButton radioBtn3;
    private javax.swing.JRadioButton radioBtn4;
    private javax.swing.JRadioButton radioBtn5;
    private javax.swing.JRadioButton radioBtn6;
    private javax.swing.JRadioButton radioBtn7;
    private javax.swing.ButtonGroup radioGroup;
    private javax.swing.JTextPane textInput;
    private javax.swing.JTextArea textOutput;
    private javax.swing.JTextArea textStackTrace;
    // End of variables declaration//GEN-END:variables
}
